<template>
    <div class="login_box">
        <div>
            <h3>嗨购商城系统</h3>
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
                <el-form-item prop="adminname">
                    <el-input :prefix-icon="User" v-model="ruleForm.adminname" type="text" autocomplete="off" />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input :prefix-icon="Lock" v-model="ruleForm.password" type="password" autocomplete="off" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">Submit</el-button>
                    <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
                </el-form-item>
            </el-form>
        </div>

    </div>
</template>

<script setup lang='ts'>
import { reactive, ref } from 'vue';
// 数据类型
import type { FormInstance, FormRules } from 'element-plus'
// 表单实例
const ruleFormRef = ref<FormInstance>()
// 引入图标
import { User, Lock } from '@element-plus/icons-vue'
// 表单数据对象
const ruleForm = reactive({
    adminname: 'admin',
    password: '123456',

})

// 验证用户名得规则
const validateAdminname = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('用户名输入有误'))
    } else {
        // 验证通过
        callback()
    }
}

// 验证密码的规则
const validatePassword = (rule: any, value: any, callback: any) => {
    // console.log('value', value);

    if (!/^\d{6}$/.test(value)) {
        callback(new Error('密码输入有误'))
    } else {
        callback()
    }
}

// 定义校验规则
const rules = reactive<FormRules<typeof ruleForm>>({
    adminname: [{ validator: validateAdminname, trigger: 'change' }],
    password: [{ validator: validatePassword, trigger: 'blur' }],
})

// 导入登录接口
import { loginApi } from '@/api/login';

// 导入 userStore.ts 仓库
import { useUserStore } from '@/stores/userstore'
const userStore = useUserStore()

// 引入useRouter函数
import { useRouter } from 'vue-router';
const router = useRouter()
// 提交表单
// 引入提示组件
import { ElMessage } from 'element-plus';
const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            // console.log('submit!')
            // console.log('ruleForm', ruleForm);
            // 调用登录接口
            loginApi(ruleForm).then((res: any) => {
                // console.log('res', res);
                // 将登录成功后的信息存储下来
                // 01: 提示一下
                ElMessage({
                    message: res.message,
                    type: res.code == 200 ? 'success' : 'error',
                    duration: 1000,
                    onClose() {
                        // 关闭时执行的回调函数
                        // 04: 跳转页面到首页
                        if (res.code == 200) {
                            router.replace('/')
                        }

                    }
                })

                if (res.code == 200) {
                    // 02: 将token 存在本地localStorageh中
                    localStorage.setItem('token', res.data.token);
                    // 03: 将用户信息存到本地的pinia仓库中
                    userStore.setAdminNameFn(res.data.adminname)
                    userStore.setCheckedKeysFn(res.data.checkedkeys)
                    userStore.setRoleFn(res.data.role)
                }

            })

        } else {
            console.log('error submit!')
            return false
        }
    })
}
// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style lang="scss" scoped>
.el-form {
    width: 400px;
    background-color: white;
    padding: 30px;
    border-radius: 10px;
}

.login_box {
    width: 100%;
    height: 100%;
    background-image: url('@/assets/image/bg.jpeg');
    background-size: 100% 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    h3 {
        text-align: center;
        line-height: 60px;
        font-size: 30px;
    }
}
</style>